package com.lemon.smartsafetynew.ui.adapter.record;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lemon.android.http.HttpUtils;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecycleChildItemClickListener;
import com.lemon.smartsafety.utils.photo.GlideImageLoader;
import com.lemon.smartsafetynew.entity.record.TankVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by lzh on 2018/3/17.
 */

public class TankInfoAdapter extends LemonRcvAdapter<TankVo> {
    private RecycleChildItemClickListener<TankVo> mListener;

    public TankInfoAdapter(@Nullable List<TankVo> data, RecycleChildItemClickListener<TankVo> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new TankItem();
    }

    public class TankItem implements AdapterItem<TankVo> {

        private TankVo data;
        private int pos;

        @Bind(R.id.record_item_iv)
        ImageView recordItemIv;
        @Bind(R.id.record_item_name_tv)
        TextView recordItemNameTv;
        @Bind(R.id.record_info_iv)
        ImageView recordInfoIv;
        @Bind(R.id.record_tank_iv)
        ImageView recordTankIv;
        @Bind(R.id.record_tank_material_name_tv)
        TextView recordTankMaterialNameTv;
        @Bind(R.id.record_class_of_dangerous_tv)
        TextView recordClassOfDangerousTv;
        @Bind(R.id.record_tank_type_tv)
        TextView recordTankTypeTv;
        @Bind(R.id.record_fire_dike_tv)
        TextView recordFireDikeTv;
        @Bind(R.id.record_volume_tv)
        TextView recordVolumeTv;
        @Bind(R.id.item_layout)
        RelativeLayout itemLayout;
        @Bind(R.id.record_tank_monitor)
        ImageView recordTankMonitor;

        @Override
        public int getLayoutResId() {
            return R.layout.layout_tank_item;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {

            itemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(data, pos);
                }
            });

            recordTankMonitor.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onChildItemClick(data, pos);
                }
            });
        }

        @Override
        public void handleData(TankVo tankVo, int position) {

            data = tankVo;
            pos = position;

            if(tankVo.getPicture() != null)
                GlideImageLoader.loadImage(recordTankIv.getContext(), HttpUtils.formatFilesUrl(tankVo.getPicture().split(",")[0]), recordTankIv, R.mipmap.high_danger);

            recordItemIv.setBackgroundResource(R.mipmap.icon_small_record_tank);
            recordItemNameTv.setText(CommonUtil.avoidNullString(tankVo.getName()));
            recordTankMaterialNameTv.setText(CommonUtil.avoidNullString(tankVo.getMaterialName()));
            recordClassOfDangerousTv.setText(CommonUtil.avoidNullString(tankVo.getDangerousChemicals()));
            recordTankTypeTv.setText(CommonUtil.avoidNullString(tankVo.getType()));
            recordFireDikeTv.setText(CommonUtil.avoidNullString(tankVo.getFireproofingDyke()));
            recordVolumeTv.setText(CommonUtil.avoidNullStringNoTitle(tankVo.getVolume()));
            if(!TextUtils.isEmpty(tankVo.getFireRating())){
                String type = tankVo.getFireRating();
                recordInfoIv.setVisibility(View.VISIBLE);
                switch (type){
                    case "甲类":
                        recordInfoIv.setImageResource(R.mipmap.record_type_1);
                        break;
                    case "乙类":
                        recordInfoIv.setImageResource(R.mipmap.record_type_2);
                        break;
                    case "丙类":
                        recordInfoIv.setImageResource(R.mipmap.record_type_3);
                        break;
                    case "丁类":
                        recordInfoIv.setImageResource(R.mipmap.record_type_4);
                        break;
                    case "戊类":
                        recordInfoIv.setImageResource(R.mipmap.record_type_5);
                        break;
                }
            }
        }
    }
}
